<?php

namespace app\common\model;

use think\Model;

/*车品超市 产品*/

class MarketModel extends Model
{
    //支付回调 修改车品订单状态 order_id 修改有该字段id的订单
    public static function marketOrder($pay_log_info, $pay_money)
    {
        $order_unified = db('market_order_unified')->where('order_no', $pay_log_info['pay_sn'])->find();
        //检查金额是否正确
        if ($order_unified['money'] != $pay_money) {
            return false;
        }
        //启动事务
        Db::startTrans();
        try {
            //查询统一订单所有的订单  进行更新付款状态和支付方式 添加支付订单号
            $pay_type = $order_unified['pay_type'];
            Db::name('market_order')->where('id', 'in', $order_unified['order_id'])->update(['status' => '1', 'pay_type' => $pay_type, 'order_no' => $pay_log_info['pay_sn'], 'paytime' => time()]);
            //新增代币使用日志
            $allDaibi = Db::name('market_order')->where('id', 'in', $order_unified['order_id'])->sum('daibi');
            //获取用户id
            $uid = Db::name('market_order')->where('id', 'in', $order_unified['orderid_str'])->avg('uid');
            //写入代币日志
            TokenMoneyModel::tokenMoneyDec($uid, $allDaibi, '车品超市抵扣');
            //写入钱包日志
            $money = $order_unified['$money'];
            $type = 1;
            $msg = '转出-车品超市';
            $order_type = 1;
            $order_id = $order_unified['order_id'];
            MoneybagLogModel::writeMoneybagLog($user_id = $uid, $money, $type, $msg, $order_type, $order_id);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
        return true;
    }
}
